<script setup>
import { Delete, Edit } from '@element-plus/icons-vue';
import {ref} from 'vue'
import ChannelSelect from './components/ChannelSelect.vue';
import { artGetListService,artDelService } from '@/api/article'
import { formatTime } from '@/utils/format';
import ArticleEdit from './components/ArticleEdit.vue'
// 表格数据
const articleList = ref([])
const total = ref(0)


//编辑逻辑
const onEditArticle=(row)=>{
    console.log(row)
}
//删除逻辑
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}


//定义请求参数对象
const params =ref({
    pagenum:1,
    pagesize:5,
    cate_id:'',
    state:''
})

//加载效果
const loading = ref(false)

//获取文章信息
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
   loading.value = false
}
getArticleList()

//分页逻辑
const onSizeChange=(size)=>{
  //size 表示当前每页的条数
  //只要是更改了当前显示的每页条数,回到第一页显示,并修改对应的 pagesize 即可
  params.value.pagenum=1
  params.value.pagesize=size
  //重新渲染文章列表
  getArticleList()
}

const onCurrentChange=(page)=>{
  //page 是点击的当前第几页
  params.pagenum=page
  //重新渲染
  getArticleList()
}


//搜索逻辑
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}

const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}



const articleEditRef = ref(null)
//抽屉逻辑--添加逻辑
const onAddarticle =()=>{
 articleEditRef.value.open({})
}
//抽屉逻辑--编辑逻辑
const onEditarticle =(row)=>{
 articleEditRef.value.open(row)
}


// 添加修改成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}

</script>

<template>
<page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddarticle"> 发布文章 </el-button>
    </template>


<!-- 表单区域 -->
    <el-form inline>
        <el-form-item label="文章分类">
           <channel-select v-model="params.cate_id"></channel-select>
           <!-- <p>当前选中频道 ID: {{ params.cate_id }}</p> -->
        </el-form-item>
        <el-form-item label="发布状态">
             <el-select style="width: 200px" v-model="params.state">
                <el-option lable="已发布" value="已发布" ></el-option>
                <el-option lable="草稿" value="草稿"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item >
            <el-button type="primary" @click="onSearch">搜索</el-button>
            <el-button @click="onReset">重置</el-button>
        </el-form-item>
    </el-form>

     <!-- 表格区域  -->
  <el-table :data="articleList" style="width: 100%"  v-loading="loading">
    <el-table-column label="文章标题" prop="title"  width="400">
        <template #default="{row}">
            <el-link type="primary" :underline="false">{{ row.title }} </el-link>
        </template>
    </el-table-column>
    <el-table-column label="分类" prop="cate_name"></el-table-column>
    <el-table-column label="发表时间"  prop="pub_date">
        <template #default="{row}">
            {{formatTime(row.pub_date) }}
        </template>

    </el-table-column>
    <el-table-column label="状态"  prop="state"></el-table-column>
    <el-table-column label="操作"  prop=""  width="100">
         <template #default="{row}">
          <el-button circle plain type="primary" :icon="Edit" @click="onEditarticle(row)"></el-button>
          <el-button circle plain type="danger" :icon="Delete" @click="onDeleteArticle(row)"></el-button>
        </template>
    </el-table-column>
  </el-table> 
  <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="background"
      layout=" jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content:flex-end"
    />

    <!-- 抽屉组件区域 -->
  <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>



 


    
  
</template>